package com.sc.nft.consume;


import com.sc.nft.entity.UserPledgeRecords;
import com.sc.nft.enums.*;
import com.sc.nft.service.PledgeTaskInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;

@RocketMQMessageListener(topic = MessageTopic.PLEDGE_TASK_AWARD_TOPIC+"${spring.profiles.active}",consumerGroup =  MessageTopic.PLEDGE_TASK_AWARD_TOPIC+"${spring.profiles.active}")
@Component
@Slf4j
public class PledgeTaskAwardConsumer implements RocketMQListener<UserPledgeRecords> {

    @Resource
    private Redisson redisson;

    @Resource
    private PledgeTaskInfoService pledgeTaskInfoService;

    @Override
    public void onMessage(UserPledgeRecords userPledgeRecords) {
        RLock lock = redisson.getLock(RedisKeyEnum.PLEDGE_TASK_AWARD_LOCK.getKey() + userPledgeRecords.getId());
        try {
            pledgeTaskInfoService.pledgeTaskAward(userPledgeRecords);
        } catch (Exception e) {
            log.error("奖励发放异常:{}",userPledgeRecords,e);
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}
